import {NgModule} from "@angular/core";
import{StoreModule,combineReducers} from "@ngrx/store";
import {StoreRouterConnectingModule} from "@ngrx/router-store";
import {StoreDevtoolsModule} from "@ngrx/store-devtools"
import * as fromQuote from "./quote.reducer";
import * as fromAuth from "./auth.reducer";
import * as fromProject from "./project.reducer";
import * as fromTaskList from "./task-list.reducer";
import * as fromTask from "./task.reducer";
import * as fromUser from "./user.reducer";
import * as authActions from "../actions/auth.action";
import {storeFreeze} from "ngrx-store-freeze";
import {compose} from "@ngrx/store";
// import {createSelector} from "reselect";`
import {createSelector} from "@ngrx/store";
import {environment} from "../../environments/environment"
import { Auth } from "../domain";
export interface State{
  quote:fromQuote.State;
  auth:Auth;
  projects:fromProject.State,
  taskLists:fromTaskList.State,
  tasks:fromTask.State,
  users:fromUser.State
}
const initialState:State={
   quote:fromQuote.initialState,
   auth:fromAuth.initialState,
   projects:fromProject.initialState,
   taskLists:fromTaskList.initialState,
   tasks:fromTask.initialState,
   users:fromUser.initialState
}
const reducers ={
  quote:fromQuote.reducer,
  auth:fromAuth.reducer,
  projects:fromProject.reducer,
  taskLists:fromTaskList.reducer,
  tasks:fromTask.reducer,
  users:fromUser.reducer,
}
// const productionReducers = combineReducers(reducers){
        
// };
//const developReducers = combineReducers(storeFreeze(reducers));
const developReducers = compose(storeFreeze,combineReducers)(reducers);
// export function reducer(state = initialState,action:any):State{

//   console.log(action);  
//   return action.type === authActions.ActionTypes.LOGOUT? initialState: environment.production?productionReducers(state,action):developReducers(state,action);
// }
// export function reducer(state = initialState,action:any):State{
//   switch (action.type) {
//     case actions.ActionTypes.LOGOUT: {
//       return initialState
//     }
//     default: {
//         return state;
//     }
//   }
// }
export function debug (reducer): any {
 return (state,action)=>{
   switch(action.type){

   }
   return reducer(state,action)
 }
}
export const meta = [debug];
export const getQuoteState = (state:State)=>state.quote;
export const getAuthState = (state:State)=>state.auth;
export const getProjectState = (state:State)=>state.projects;
export const getTaskListState = (state:State)=>state.taskLists;
export const getTasksState = (state:State)=>state.tasks;
export const getUserState = (state:State)=>state.users;

export const getQuote = createSelector(getQuoteState,fromQuote.getQuote);
export const getProjects = createSelector(getProjectState,fromProject.getAll );
export const getTaskLists = createSelector(getTaskListState,fromTaskList.getSelected );
export const getTasks = createSelector( getTasksState, fromTask.getTasks);
export const getUsers = createSelector( getUserState, fromUser.getUsers );

export const getUserEntities = createSelector(getUserState, fromUser.getEntities);
export const getTaskWithOwners = createSelector (getTasks,getUserEntities,(tasks,userEntities)=>{
  console.log(tasks,userEntities)
  return tasks.map(task =>{
    return {
      ...task,
      owner: userEntities[task.ownerId],
      participants: task.participantIds.map(id => userEntities[id])
    }
  })
});
export const getTasksByLists = createSelector(getTaskLists, getTaskWithOwners,(lists,tasks)=>{
  console.log(lists,tasks);
  
  return lists.map(list=>{
    return {
      ...list,
      tasks:tasks.filter(task=>task.taskListId ===list.id)  
    }
  })
});
export const getProjectUsers = (projectId:string) => createSelector(getProjectState,getUserEntities,(state,entities)=>{
  return state.entities[projectId].members.map(id => entities[id]);
})
@NgModule({
   imports:[
     StoreModule.forRoot(reducers),
     StoreRouterConnectingModule.forRoot(),
     StoreDevtoolsModule.instrument()
   ]
})
export class AppStoreModule{}
